﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [LastModified("2022-5-12", "create DistortionBlurPage.xaml")]
    [PageType("cpu", "扭曲效果")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class DistortionBlurPage : Page
    {



        LayerBitmapData bitmapData = new LayerBitmapData();


        public DistortionBlurPage()
        {
            this.InitializeComponent();


            Init();
        }

        private void Init()
        {
            selectPicture.Click += async (s, e) => { await bitmapData.SelectOneBitmapAsync(); canvas.Invalidate(); };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null)
                {
                    var element = (FrameworkElement)s;
                    float effectWidth = (float)element.ActualWidth;
                    float effectHeight = (float)element.ActualHeight * 0.7f;
                    float previewWidth = effectWidth;
                    float previewHeight = (float)element.ActualHeight * 0.3f;


                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);

                    double va = angle.Value;
                    //角度
                    double ang = ((double)(-va / 180)) * Math.PI;
                    double icenterX = bitmapData.oneWidth * 0.5;
                    double icenterY = bitmapData.oneHeight * 0.5;
                    double radius = Math.Sqrt(Math.Pow(icenterX, 2) + Math.Pow(icenterY, 2));
                    var resultColors = PictureFilter.DistortionEffect(bitmapData.oneColors, bitmapData.oneWidth, bitmapData.oneHeight, va, radius, new Point(icenterX, icenterY));
                    var effectBitmap = CanvasBitmap.CreateFromColors(canvas, resultColors, bitmapData.oneWidth, bitmapData.oneHeight);
                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    effectTran.Source = effectBitmap;
                    e.DrawingSession.DrawImage(effectTran);
                }
            };

            RangeBaseValueChangedEventHandler ValueChanged = (sender, e) =>
            {
                canvas.Invalidate();
            };

            angle.ValueChanged += ValueChanged;


            distortion1.Click += (s, e) => { canvas.Invalidate(); };
        }

   
    }
}
